<% if false # the license inside this if block assertains to this file -%>
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
<% end -%>
<% inside_indent = (test_matrix.level + 2) * 2 -%>
<%
  config_path = if name
                  %w[present exist success title_and_name before]
                else
                  %w[present exist success title_eq_name before]
                end

  cust_before = get_code_multiline(tests, config_path)

  # Build out a graph of objects.
  collector = Dependencies::DependencyGraph.new(@data_gen)
  (0..2).each do |index|
    collector.add(object, index, (name ? :name : :title), ensure: 'present')
  end

  # Generate the Puppet manifest using the graph of objects above.
  # This manifest will be ordered by dependencies.
  resources = manifester.generate_all_objects(collector, object.name,
                                              name ? :name : :title,
                                              ensure: 'present')


  # Creates the expect_network_* statements required for this test.
  # This includes the expect statements for all referenced resources.
  expectations = [
    # Generate network expectations for 3 objects
    (1..3).map do |idx|
      prop_data.create_expectation('expect_network_get_success', name, object,
                                   inside_indent, [], idx)
    end,
    prop_data.create_resource_ref_get_success(object, collector, inside_indent)
  ].flatten.compact.uniq

  resource_block = resources.flatten(1).uniq.map { |r| lines(r) }.join("\n")
  res_name = "#{object.out_name.capitalize}[title0]"

  # TODO(nelsonjr): Fix test when object references itself as a ResourceRef,
  # e.g. a bucket ACL points back to a bucket as a resource ref. When generating
  # the references the code it oblivious to that fact and attempt to have
  # various objects created to satisfy the dependency. That leads to collision
  # of object seed==0, as well as not having objects being tested created to
  # satisty dependency.
  references_self_type = false
  collector.each do |obj|
    references_self_type = true if obj.parent && \
      obj.parent.__resource == obj.object
  end
-%>
<% if !cust_before.nil? -%>
            before do
<%= lines(indent(cust_before, inside_indent))  -%>
            end
<% else -%>
            before do
              allow(Time).to receive(:now).and_return(
                Time.new(2017, 1, 2, 3, 4, 5)
              )
<%= lines(indent(expectations, inside_indent)) -%>
            end
<% end -%>

            let(:catalog) do
              apply_with_error_check(
                <<-MANIFEST
<%= lines(indent(resource_block, inside_indent + 2)) -%>
                MANIFEST
              ).catalog
            end
<% (0..2).each do |index| -%>
<% res_name = "#{object.out_name.capitalize}[title#{index}]" -%>

            context '<%= res_name -%>' do
              subject do
<%=
  lines(format([
          ["catalog.resource('#{res_name}').provider"],
          [
            "catalog.resource('#{res_name}')",
            "       .provider"
          ]
        ], 16))
-%>
              end

<% if references_self_type -%>
              it 'was expected to be present', broken: true do
                pending('Implement tests where object references its own type.')
              end
<% else -%>
<%
     object.properties.each do |prop|
       if !name && prop.name == 'name'
-%>
              it { is_expected.to have_attributes(name: 'title<%= index -%>') }
<%
         next
       end
-%>
<%=    lines(indent(@property.property(prop, index,
                                       @data_gen.comparator(prop),
                                       @data_gen.value(prop.class, prop, index),
                                        inside_indent),
                    inside_indent)) -%>
<%   end # if !name-%>
<% end # if references_self_type -%>
            end
<% end # index -%>
